package com.ibm.ops;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import com.ibm.ops.domain.OpsAuditLogMetaData;
import com.ibm.ops.domain.OpsLogMetaDataType;
import com.ibm.ops.exception.ReadAuditLogIOException;

class OpsAuditLog{
	
	private String source_path = "";
	
	private OpsAuditLogStore store = null;
	
	OpsAuditLog(String source_path){
		this.source_path = source_path;
		this.store = new OpsAuditLogStore();
	}
	
	private OpsLogMetaDataType currentstate = null;

	/**
	 * Separate the log file to 5 parts when loading the log file.
	 */
	void parseLog() throws ReadAuditLogIOException{
		
		File source = new File(source_path);		
		BufferedReader br = null;
		String line = "";
		StringBuffer sb = null;
		boolean isstart = false;
		try {
			br = new BufferedReader(new FileReader(source));
			while((line = br.readLine()) != null){				
				if(OpstoolConstant.HUB_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					sb = new StringBuffer();					
					currentstate = OpsLogMetaDataType.HUB;
				}else if(OpstoolConstant.CIR_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					if(sb != null){
//						System.out.println("INFORSOURCE :" + sb);
						this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
					}
					sb = new StringBuffer();
					currentstate = OpsLogMetaDataType.CIR;
				}else if(OpstoolConstant.INFOSOURCE_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					if(sb != null){
//						System.out.println("HUB :" + sb);
						this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
					}
					sb = new StringBuffer();
					currentstate = OpsLogMetaDataType.INFOSOURCE;
				}else if(OpstoolConstant.FODS_REJECT_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					if(sb != null){
//						System.err.println("CIR :" + sb);
						this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
					}
					sb = new StringBuffer();
					currentstate = OpsLogMetaDataType.FODS_REJECT;
				}else if(OpstoolConstant.FODS_OUTOF_SEN_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					if(sb != null){
//						System.err.println("FODS Reject :" + sb);
						this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
					}
					sb = new StringBuffer();
					currentstate = OpsLogMetaDataType.FODS_OUTOF_SEQUENCE;
				}else if(OpstoolConstant.FODS_NOORDER_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					if(sb != null){
//						System.err.println("FODS Out of seq:" + sb);
						this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
					}
					sb = new StringBuffer();
					currentstate = OpsLogMetaDataType.FODS_NO_ORDER;
				}else if(OpstoolConstant.FODS_EOD_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					if(sb != null){
//						System.err.println("FODS No Order :" + sb);
						this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
					}
					sb = new StringBuffer();
					currentstate = OpsLogMetaDataType.FODS_EOD;
				}else if(OpstoolConstant.FOOT_START_TAG.equalsIgnoreCase(line.trim())){
					isstart = true;
					if(sb != null){
//						System.err.println("FODS EOD :" + sb);
						this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
					}
					sb = new StringBuffer();
					currentstate = OpsLogMetaDataType.FOOT;
				}
				if(isstart){
					if(sb != null){
						sb.append(line).append(OpstoolConstant.SYSTEM_LINE_SEPARATOR);
					}
				}				
			}
			if(sb != null){
//				System.out.println("FOOT :" + sb);
				this.store.addOpsAuditLogMetaData(new OpsAuditLogMetaData(sb.toString(),currentstate));
			}
			br.close();
		} catch (IOException e) {
			throw new ReadAuditLogIOException();
		} finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void setStore(OpsAuditLogStore store) {
		this.store = store;
	}
}
